<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
      <el-table-column label="分组名称" align="center" prop="groupName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleManage(scope.row)"
            v-hasPermi="['bizsys:group:insert']">管理</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <el-dialog :title="groupUser.title" :visible.sync="groupUser.open" width="1000px" append-to-body>
      <div class="app-container">
        <el-form :model="groupUser.queryParams" ref="queryForm" :inline="true" v-show="groupUser.showSearch" label-width="100px">
          <el-form-item label="Userid" prop="userId">
            <el-input v-model="groupUser.queryParams.userId" placeholder="Userid" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="groupUser.queryParams.phone" placeholder="手机号" clearable size="small"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
              v-hasPermi="['bizsys:group:remove']">移除</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="groupUser.showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="groupUser.groupUserList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="Userid" align="center" prop="userId" />
          <el-table-column label="手机号" align="center" prop="phone" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                v-hasPermi="['bizsys:group:remove']">移除分组</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listGroup,
    listGroupUser,
    delGroupUser,
  } from "@/api/bizsys/award_group";

  export default {
    name: "Group",
    components: {},
    data() {
      return {
        // 标签用户列表
        groupUser: {
          // 选中数组
          ids: [],
          // 非单个禁用
          single: true,
          // 非多个禁用
          multiple: true,
          // 遮罩层
          loading: true,
          title: '',
          open: false,
          queryParams: {
            pageNum: 1,
            pageSize: 10,
            awardGroupCode:'',
            userId:null,
            phone:null,
          },
          // 总条数
          total: 0,
          // 奖励分组表格数据
          groupUserList: [],
          // 显示搜索条件
          showSearch: true,
        },
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 奖励分组表格数据
        groupList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
      };
    },
    created() {
      // this.getList();
    },
    methods: {
      /** 查询奖励分组列表 */
      getList() {
        this.loading = true;
        listGroup(this.queryParams).then(response => {
          this.groupList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询奖励分组列表 */
      getListGroupUser() {
        this.groupUser.loading = true;
        listGroupUser(this.groupUser.queryParams).then(response => {
          this.groupUser.groupUserList = response.rows;
          this.groupUser.total = response.total;
          this.groupUser.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.groupUser.queryParams.pageNum = 1;
        this.getListGroupUser();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.groupUser.single = selection.length !== 1
        this.groupUser.multiple = !selection.length
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认要将用户移除分组', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delGroupUser(ids);
        }).then(() => {
          this.getListGroupUser();
          this.msgSuccess("删除成功");
        })
      },
      /** manage */
      handleManage(row) {
        this.groupUser.open = true;
        this.groupUser.title = row.groupName;
        this.groupUser.queryParams.awardGroupCode = row.groupCode;
        this.resetQuery();
      }
    }
  };
</script>
